package com.owen.springboot.smartparkingms.module.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.module.app.entity.Park;
import com.owen.springboot.smartparkingms.module.app.entity.ParkArea;
import com.owen.springboot.smartparkingms.module.app.entity.ParkStatus;
import com.owen.springboot.smartparkingms.module.app.service.IParkAreaService;
import com.owen.springboot.smartparkingms.module.app.service.IParkService;
import com.owen.springboot.smartparkingms.module.app.service.IParkStatusService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/app/parkArea")
public class ParkAreaController {

    @Resource
    IParkAreaService parkAreaService;

    @Resource
    IParkStatusService parkStatusService;

    @Resource
    IParkService parkService;
    /**
     * 列表
     */
    @PostMapping("list")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result list(ParkArea parkArea){
        return parkAreaService.listPage(parkArea);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result save(@RequestBody ParkArea parkArea){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("park_id", parkArea.getParkId());
        Park park = parkService.getOne(queryWrapper);

        int spaceNum = parkArea.getRowNum()*parkArea.getColumnNum();
        if(parkArea.getId()!=null){
            ParkArea parkArea1 = parkAreaService.getById(parkArea.getId());
            int spaceNum1 = parkArea1.getRowNum()*parkArea1.getColumnNum();
            for(int j = 1; j <= spaceNum1; j++){
                QueryWrapper q = new QueryWrapper();
                q.eq("space_id", parkArea1.getArea() + "-" + j);
                parkStatusService.remove(q);
            }
            park.setParkingSpaceNumber(park.getParkingSpaceNumber() - spaceNum1);
            parkService.saveOrUpdate(park);
        }
        parkArea.setPlace((long) (spaceNum));
        parkArea.setParkId(parkArea.getParkId());
        List<ParkStatus> list = new ArrayList();
        for (int i = 1; i <= spaceNum; i++) {
            ParkStatus parkStatus = new ParkStatus();
            parkStatus.setStatus("空位");
            parkStatus.setParkId(parkArea.getParkId());
            parkStatus.setSpaceId(parkArea.getArea() + "-" + i);
            list.add(parkStatus);
        }
        parkStatusService.saveOrUpdateBatch(list);

        QueryWrapper q = new QueryWrapper();
        q.eq("park_id", parkArea.getParkId());
        Park park1 = parkService.getOne(q);
        if(park1.getParkingSpaceNumber() == null){
            park1.setParkingSpaceNumber(spaceNum);
        }else {
            park1.setParkingSpaceNumber(park1.getParkingSpaceNumber() + spaceNum);
        }
        parkService.saveOrUpdate(park1);

        parkAreaService.saveOrUpdate(parkArea);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result delete(Long id,Long parkId){
        ParkArea parkArea1 = parkAreaService.getById(id);
        int spaceNum1 = parkArea1.getRowNum()*parkArea1.getColumnNum();
        for(int j = 1; j <= spaceNum1; j++){
            QueryWrapper q = new QueryWrapper();
            q.eq("space_id", parkArea1.getArea()+"-"+j);
            parkStatusService.remove(q);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("park_id", parkArea1.getParkId());
        Park park = parkService.getOne(queryWrapper);
        park.setParkingSpaceNumber(park.getParkingSpaceNumber() - spaceNum1);
        parkService.saveOrUpdate(park);

        parkAreaService.removeById(id);
        return Result.ok();
    }

    /**
     * 获取
     */
    @PostMapping("get")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result get(Long id){
        ParkArea parkArea = parkAreaService.getById(id);
        if(parkArea != null){
            return Result.ok(parkArea);
        }else {
            return Result.ok(new ParkArea());
        }
    }
}
